brute force constructive algorithms graphs implementation

Please click on ads to support us..

Python Code:

t = int(input())
while t:
    n, m = input().split()
    n = int(n)
    m = int(m)
    c = n
    grid = []
    while c:
        s = input()
        grid.append(s)
        c = c - 1
    for i in range(n):
        grid[i] = list(grid[i])
    for i in range(n):
        for j in range(m):
            grid[i][j] = int(grid[i][j])
    found = 0
    for i in range(n):
        for j in range(m):
            s = grid[i][j]
            if i != n - 1:
                s += grid[i + 1][j]
            if j != m - 1:
                s += grid[i][j + 1]
            if i != n - 1 and j != m - 1:
                s += grid[i + 1][j + 1]
            if s == 3:
                found = 1
                break
    if found == 1:
        print('NO')
    else:
        print('YES')
    t = t - 1

C++ Code:

// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;
// const int N=1e5+10;
// vector<int> graph[N];

int main() {
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        vector<string> st;
        for(int i=0;i<n;i++){
            string s;
            cin>>s;
            st.push_back(s);
        }
        bool flag=false;
        for(int i=0;i<n-1;i++){
            for(int j=0;j<m-1;j++){
                int sum = st[i][j]+st[i+1][j]+st[i][j+1]+st[i+1][j+1] - 4*(48);
                if(sum == 3){ flag = true;break;}
            }
            if(flag==true) break;
        }
        if(!flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

908A - New Year and Counting Cards
146A - Lucky Ticket
1594C - Make Them Equal
1676A - Lucky
1700B - Palindromic Numbers
702C - Cellular Network
1672C - Unequal Array
1706C - Qpwoeirut And The City
1697A - Parkway Walk
1505B - DMCA
478B - Random Teams
1705C - Mark and His Unfinished Essay
1401C - Mere Array
1613B - Absent Remainder
1536B - Prinzessin der Verurteilung
1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64
755B - PolandBall and Game
808B - Average Sleep Time
1515E - Phoenix and Computers